package com.xuecheng.orders.api;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.orders.config.AlipayConfig;
import com.xuecheng.orders.model.dto.AddOrderDto;
import com.xuecheng.orders.model.dto.PayRecordDto;
import com.xuecheng.orders.model.po.XcPayRecord;
import com.xuecheng.orders.service.OrderService;
import com.xuecheng.orders.util.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @description: 订单相关的接口
 * @author: ajie
 * @date: 2024/2/3 20:31:33
 * @version: 1.0
 */
@Api(value = "订单支付接口", tags = "订单支付接口")
@Slf4j
@Controller
public class OrderController {

    @Autowired
    OrderService orderService;



    /**
     * @description: 生成支付二维码 
     * @params: [addOrderDto]       
     * @return: 
     * @author: ajie
     * @date: 2024/2/4 18:33
     */
    @ApiOperation("生成支付二维码")
    @PostMapping("/generatepaycode")
    @ResponseBody
    public PayRecordDto generatePayCode(@RequestBody AddOrderDto addOrderDto) {

        SecurityUtil.XcUser user = SecurityUtil.getUser();
        String userId = user.getId();

        //调用service，完成插入订单信息、插入支付记录、生成支付二维码
        PayRecordDto payRecordDto = orderService.createOrder(userId,addOrderDto);

        return payRecordDto;
    }

    /**
     * @description: 扫码下单接口 
     * @params: [payNo, httpResponse]       
     * @return: 
     * @author: ajie
     * @date: 2024/2/4 18:33
     */
    @ApiOperation("扫码下单接口")
    @GetMapping("/requestpay")
    public void requestpay(String payNo, HttpServletResponse httpResponse) throws IOException, AlipayApiException {

        orderService.requestpay(payNo,httpResponse);

    }

    /**
     * @description: 查询支付结果
     * @params: [payNo]
     * @return:
     * @author: ajie
     * @date: 2024/2/5 20:33
     */
    @ApiOperation("查询支付结果")
    @GetMapping("/payresult")
    @ResponseBody
    public PayRecordDto payresult(String payNo) throws IOException {

        //查询支付结果
        PayRecordDto payRecordDto = orderService.queryPayResult(payNo);

        return payRecordDto;

    }

    /**
     * @description: 接收通知
     * @params: [request, response]
     * @return:
     * @author: ajie
     * @date: 2024/2/5 20:33
     */
    @PostMapping("/paynotify")
    public void paynotify(HttpServletRequest request, HttpServletResponse response) throws IOException, AlipayApiException{

        orderService.paynotify(request,response);



    }




}
